function main(defaultParams) {
    debugger
    const self = this;
    const root = self.$$roots()[0];
    const tableComp = root.$$("table_hpjsx7");
    const pageListEle = self.$$("page_widget_list_cjxh15");
    const listRenderView = pageListEle.$$getRenderElement();
    const currentListEle = listRenderView.$$getListElement();
    const selection = tableComp.$$getSelection();
    const selectedRows = selection.selectedRows;
    const dataset = tableComp.$$getDataset();

    console.log({selectedRows: selectedRows, dataset: dataset});

    if (!selectedRows.length) {
        self.$message.warning("请选择要提交申请的人员！");
        return;
    }

    if (!canSubmitRows(selectedRows)) {
        self.$message.warning("只能对待提交和审核未通过数据进行提交申请！");
        return;
    }

    tableComp.loading = true;
    showConfirmation(self)
        .then(function (confirmed) {
            if (!confirmed) {
                tableComp.loading = false;
                return Promise.reject("cancelled");
            }

            return Promise.all(
                selectedRows.map(function (row) {
                    return processRowSubmission(self, defaultParams, row);
                })
            ).then(function (results) {
                self.$message.success("提交成功！");
                return refreshTable(self, currentListEle, tableComp);
            })
        })

        .catch(function (error) {
            if (error !== "cancelled") {
                self.$message.error("提交失败: " + (error.message || "未知错误"));
                tableComp.loading = false;
            }
        });
}

function canSubmitRows(rows) {
    return rows.every(function (row) {
        return ["0", "3"].indexOf(row.qualification_code.value) !== -1;
    });
}

function showConfirmation(self) {
    return new Promise(function (resolve) {
        self.$lc_Confirm({
            content: "确定发起资格申请上报流程？",
            type: "info",
            headerTitle: "提示",
            showOkBtn: true,
            showCancelBtn: true,
            cancelText: "取消",
            okText: "确定",
            onOk: function () {
                resolve(true);
            },
            onCancel: function () {
                resolve(false);
            }
        });
    });
}

function processRowSubmission(self, defaultParams, row) {
    var documentNo = row.cardId.value;
    var identityNo = row.identity_no.value;

    return submitProcess(self, defaultParams, documentNo)
        .then(function (submissionRes) {
            let submissionFlag = JSON.parse(submissionRes.result).hasError;
            if (!submissionFlag) {
                return updateQualificationStatus(self, identityNo)
                    .then(function (updateRes) {
                        return submissionRes;
                    });
            }
            return submissionRes;
        });
}

function updateQualificationStatus(self, identityNo) {
    var formula = "UPDATE('t_ecdf9c71ff624a45a65adb0ece7a64a8', " +
        "QUERY_EQ('identity_no','" + identityNo + "'), '', " +
        "['qualification_code', 1, 'qualification_name', '待审核'])";

    return api.EXEC_FORMULA(formula)
        .then(function (res) {
            if (res && res.result) {
                console.log("数据修改成功");
            }
            return res;
        });
}

function refreshTable(self, listElement, tableComp) {
    return new Promise(function (resolve) {
        setTimeout(function () {
            self.$nextTick(function () {
                listElement.$$refreshData();
                console.log("数据刷新成功");
                tableComp.loading = false;
                tableComp.$$clearSelection();
                resolve();
            });
        }, 2000);
    });
}

function submitProcess(self, defaultParams, documentNo) {
    var queryParams = [{
        name: "pageId",
        valueContent: "a7a88151-bd54-42c5-9520-249ebbbe5c45"
    }];

    var jsonParams = {documentNo: documentNo};

    return api.DATA_SOURCE_EXECUTE(
        "a2b29830-9ef5-437a-8975-476783d4e73f",
        "api",
        queryParams,
        [],
        JSON.stringify(jsonParams)
    ).then(function (res) {
        console.log("响应", res);
        if (res && res.hasError) {
            throw new Error(res.message);
        }
        return res;
    }).catch(function (error) {
        self.$message.error(error.message);
        throw error;
    });
}
